<template>
	<view>
		<view class="goods-list">
			<block v-for="(item,i) in goodsList" :key="i">
				<my-goods :goods='item' @click.native="gotoGoodsDetail(item.goods_id)"></my-goods>
			</block>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj: {
					query: '',
					cid: '',
					pagenum: 1,
					pagesize: 10
				},
				goodsList: [],
				total: 0,
				isLoading: false // 请求状态  true关闭
			};
		},
		onLoad(options) {
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.id || ''

			this.getgoodsList()
		},
		// 上拉加载事件  pages.json里定义页面底部触发距离 onReachBottomDistance
		onReachBottom() {
			if (this.isLoading) return uni.$showToast('点慢点')

			if (this.goodsList.length >= this.total) return uni.$showToast('数据加载完毕')

			this.queryObj.pagenum++
			this.getgoodsList()
		},
		// 下拉刷新事件 	pages.json里 enablePullDownRefresh: true
		onPullDownRefresh() {
			this.goodsList = []
			this.isLoading = false
			this.total = 0
			this.queryObj.pagenum = 1
			this.getgoodsList(() => {
				uni.stopPullDownRefresh() // 终止下拉
			})
		},
		methods: {
			async getgoodsList(callback) {
				this.isLoading = true // 关闭
				const {
					data
				} = await uni.$http.get('/goods/search', this.queryObj)
				if (data.meta.status !== 200) return uni.$showToast()
				this.goodsList = [...this.goodsList, ...data.message.goods]
				this.total = data.message.total
				this.isLoading = false // 打开

				typeof callback === 'function' && callback()
				// if (typeof callback === 'function') {
				// 	callback()
				// }
			},
			gotoGoodsDetail(goods_id) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods_id
				})
			}
		}

	}
</script>

<style lang="scss">

</style>